package titt.server.func.util;

import titt.head.HeadFactory;
import titt.head.request.MethodHead;
import titt.head.response.ResponseHead;
import titt.io.socket.TITTSocket;
import titt.io.writer.ResponseWriter;
import titt.key.TITTKey;
import titt.server.func.TITTFunction;
import titt.server.os.ObjectServer;
import titt.server.service.Service;
import titt.type.TypedObject;
import titt.type.Types;

public class ListServices implements TITTFunction {

	public String getMethodName() {
		return "list";
	}

	public void handle(TITTKey key, MethodHead head, TITTSocket ts, ObjectServer os) throws Exception {
		
		Service[] total = os.getServerDatabase().getAll();
		
		String[] totalKeys = new String[total.length];
		
		for(int i = 0; i < total.length; i++)
			totalKeys[i] = total[i].getKey().asKeyString();
		
		ResponseWriter rw = new ResponseWriter(ts, os.getEncodingFacade(), new ResponseHead(200, HeadFactory.EMPTY));
		rw.writeValues(new TypedObject(Types.toArray(Types.STRING), totalKeys));
		
		ts.close();
		
	}

}
